<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="field[@type = 'double_foreign_key' and parent::doctype]">
		<script type="text/javascript" src="{$static_prefix}/js/double_foreign_key_dtf.js"/>
		<xsl:variable name="is_new" select="parent::doctype/@action = 'create'"/>
		<xsl:variable name="document_field" select="../../document[@doctype_name = current()/parent::doctype/@name]/field[@name = current()/@name]"/>
		<div class="field dn" id="group_field_{@name}">
			<label class="title_label" for="group_{@name}">
				<xsl:value-of select="@title"/>
				<xsl:text>:&#160;</xsl:text>
				<xsl:if test="@is_important = 1">
					<span class="star">*</span>
				</xsl:if>
			</label>
			<select id="group_{@name}" name="group_{@name}" onchange="double_foreign_key_dtf_show_item('{@name}', document.getElementById('group_{@name}').options[document.getElementById('group_{@name}').selectedIndex].value)">
				<option value="">Не выбрано</option>
				<xsl:for-each select="group">
					<xsl:choose>
						<xsl:when test="false"/>
						<!--<xsl:when test="@value = '0'">
								<xsl:for-each select="item">
									<option value="{@value}" style="padding-left:20px">
										<xsl:choose>
											<xsl:when test="../../../../pass_info/vars/var[@name = current()/../../@name] = @value">
												<xsl:attribute name="selected">selected</xsl:attribute>
											</xsl:when>
											<xsl:when test="../../../../pass_info/vars/var[@name = current()/../../@name]"/>
											<xsl:when test="$document_field/@item_value_key = @value">
												<xsl:attribute name="selected">selected</xsl:attribute>
											</xsl:when>
										</xsl:choose>
										<xsl:value-of select="@title"/>
									</option>
								</xsl:for-each>
							</xsl:when>-->
						<xsl:otherwise>
							<option value="{@value}">
								<xsl:choose>
									<xsl:when test="../../../pass_info/vars/var[@name = concat('group_', current()/../@name)] = @value">
										<xsl:attribute name="selected">selected</xsl:attribute>
									</xsl:when>
									<xsl:when test="../../../pass_info/vars/var[@name = concat('group_', current()/../@name)]"/>
									<xsl:when test="$document_field/@group_value_key = @value">
										<xsl:attribute name="selected">selected</xsl:attribute>
									</xsl:when>
								</xsl:choose>
								<xsl:value-of select="@title"/>
							</option>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:for-each>
			</select>
			<xsl:if test="../../pass_info/error[@field_name = concat('group_', current()/@name)]">
				<xsl:choose>
					<xsl:when test="../../pass_info/error[@field_name = concat('group_', current()/@name) and @name = 'UNFILLED']">
						<div class="error">
							<span>Это обязательное поле, и оно должно быть заполнено</span>
						</div>
					</xsl:when>
					<xsl:when test="../../pass_info/error[@field_name = concat('group_', current()/@name) and @name = 'UNEXISTED_VALUE']">
						<div class="error">
							<span>Выбрано несуществующее значение. Попробуйте ещё раз</span>
						</div>
					</xsl:when>
				</xsl:choose>
			</xsl:if>
		</div>
		<script type="text/javascript">
			document.getElementById('group_field_<xsl:value-of select="@name"/>').style.display = 'block';
		</script>
		<xsl:for-each select="group">
			<xsl:choose>
				<xsl:when test="@value = '0'">
					<div class="field dn" id="group_{current()/../@name}_{@value}">
						<label for="item_{current()/../@name}_{@value}">
							<xsl:value-of select="current()/../@group_title"/>
							<xsl:text>:&#160;</xsl:text>
							<xsl:if test="current()/../@is_important = 1">
								<span class="star">*</span>
							</xsl:if>
						</label>
						<input class="string-" type="text" name="{current()/../@group_other_name}" value="{$document_field/@group_other_value}">
							<xsl:if test="../../../pass_info/vars/var[@name = current()/../@group_other_name]">
								<xsl:attribute name="value">
									<xsl:value-of select="../../../pass_info/vars/var[@name = current()/../@group_other_name]"/>
								</xsl:attribute>
							</xsl:if>
						</input>
						<xsl:if test="../../../pass_info/error[@field_name = current()/../@group_other_name]">
							<xsl:choose>
								<xsl:when test="../../../pass_info/error[@field_name = current()/../@group_other_name and @name = 'UNFILLED']">
									<div class="error">
										<span>Это обязательное поле, и оно должно быть заполнено</span>
									</div>
								</xsl:when>
								<xsl:when test="../../../pass_info/error[@field_name = current()/../@group_other_name and @name = 'UNEXISTED_VALUE']">
									<div class="error">
										<span>Выбрано несуществующее значение. Попробуйте ещё раз</span>
									</div>
								</xsl:when>
							</xsl:choose>
						</xsl:if>
					</div>
					<div class="field dn" id="item_{current()/../@name}_{@value}">
						<label for="item_{current()/../@name}_{@value}">
							<xsl:value-of select="current()/../@item_title"/>
							<xsl:text>:&#160;</xsl:text>
							<xsl:if test="current()/../@is_important = 1">
								<span class="star">*</span>
							</xsl:if>
						</label>
						<input class="string-" type="text" name="{current()/../@item_other_name}" value="{$document_field/@item_other_value}">
							<xsl:if test="../../../pass_info/vars/var[@name = current()/../@item_other_name]">
								<xsl:attribute name="value">
									<xsl:value-of select="../../../pass_info/vars/var[@name = current()/../@item_other_name]"/>
								</xsl:attribute>
							</xsl:if>
						</input>
						<xsl:if test="$module/pass_info/error[@field_name = current()/../@item_other_name]">
							<xsl:choose>
								<xsl:when test="../../../pass_info/error[@field_name = current()/../@item_other_name and @name = 'UNFILLED']">
									<div class="error">
										<span>Это обязательное поле, и оно должно быть заполнено</span>
									</div>
								</xsl:when>
								<xsl:when test="../../../pass_info/error[@field_name = current()/../@item_other_name and @name = 'UNEXISTED_VALUE']">
									<div class="error">
										<span>Выбрано несуществующее значение. Попробуйте ещё раз</span>
									</div>
								</xsl:when>
							</xsl:choose>
						</xsl:if>
					</div>
				</xsl:when>
				<xsl:otherwise>
					<div class="field dn" id="item_{current()/../@name}_{@value}">
						<label for="item_{current()/../@name}_{@value}">
							<xsl:value-of select="current()/../@item_title"/>
							<xsl:text>:&#160;</xsl:text>
							<xsl:if test="current()/../@is_important = 1">
								<span class="star">*</span>
							</xsl:if>
						</label>
						<select name="item_{current()/../@name}_{@value}">
							<option value="">Не выбрано</option>
							<xsl:for-each select="item">
								<option value="{@value}">
									<xsl:choose>
										<xsl:when test="../../../../pass_info/vars/var[@name = concat('item_', current()/../../@name, '_', current()/../@value)] = @value">
											<xsl:attribute name="selected">selected</xsl:attribute>
										</xsl:when>
										<xsl:when test="../../../../pass_info/vars/var[@name = concat('item_', current()/../../@name, '_', current()/../@value)]"/>
										<xsl:when test="$document_field/@item_value_key = @value">
											<xsl:attribute name="selected">selected</xsl:attribute>
										</xsl:when>
									</xsl:choose>
									<xsl:value-of select="@title"/>
								</option>
							</xsl:for-each>
						</select>
						<xsl:if test="../../../pass_info/error[@field_name = concat('item_', current()/../@name, '_', current()/@value)]">
							<xsl:choose>
								<xsl:when test="../../../pass_info/error[@field_name = concat('item_', current()/../@name, '_', current()/@value) and @name = 'UNFILLED']">
									<div class="error">
										<span>Это обязательное поле, и оно должно быть заполнено</span>
									</div>
								</xsl:when>
								<xsl:when test="../../../pass_info/error[@field_name = concat('item_', current()/../@name, '_', current()/@value) and @name = 'UNEXISTED_VALUE']">
									<div class="error">
										<span>Выбрано несуществующее значение. Попробуйте ещё раз</span>
									</div>
								</xsl:when>
							</xsl:choose>
						</xsl:if>
					</div>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:for-each>
		<script type="text/javascript">
			double_foreign_key_dtf_show_item('<xsl:value-of select="@name"/>', document.getElementById('group_<xsl:value-of select="@name"/>').options[document.getElementById('group_<xsl:value-of select="@name"/>').selectedIndex].value);
		</script>
		<noscript>
			<input type="hidden" name="noscript" value="1"/>
			<!--Если нет javascript, то мы отображаем один select, где есть группы -->
			<!--Я этого не тестировал после изменений, работать не должно -->
			<div class="field">
				<label for="{@name}">
					<xsl:value-of select="@title"/>
					<xsl:text>:&#160;</xsl:text>
					<xsl:if test="@is_important = 1">
						<span class="star">*</span>
					</xsl:if>
				</label>
				<select name="{@name}">
					<option value="">Не выбрано</option>
					<xsl:for-each select="group">
						<xsl:choose>
							<xsl:when test="@value = '0'">
								<xsl:for-each select="item">
									<option value="{@value}" style="padding-left:20px">
										<xsl:choose>
											<xsl:when test="../../../../pass_info/vars/var[@name = current()/../../@name] = @value">
												<xsl:attribute name="selected">selected</xsl:attribute>
											</xsl:when>
											<xsl:when test="../../../../pass_info/vars/var[@name = current()/../../@name]"/>
											<xsl:when test="$document_field/@item_value_key = @value">
												<xsl:attribute name="selected">selected</xsl:attribute>
											</xsl:when>
										</xsl:choose>
										<xsl:value-of select="@title"/>
									</option>
								</xsl:for-each>
							</xsl:when>
							<xsl:otherwise>
								<optgroup label="{@title}">
									<xsl:for-each select="item">
										<option value="{@value}" style="padding-left:20px">
											<xsl:choose>
												<xsl:when test="../../../../pass_info/vars/var[@name = current()/../../@name] = @value">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:when>
												<xsl:when test="../../../../pass_info/vars/var[@name = current()/../../@name]"/>
												<xsl:when test="$document_field/@item_value_key = @value">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:when>
											</xsl:choose>
											<xsl:value-of select="@title"/>
										</option>
									</xsl:for-each>
								</optgroup>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:for-each>
				</select>
				<xsl:if test="../../pass_info/error[@field_name = current()/@name]">
					<xsl:choose>
						<xsl:when test="../../pass_info/error[@field_name = current()/@name and @name = 'UNFILLED']">
							<div class="error">
								<span>Это обязательное поле, и оно должно быть заполнено</span>
							</div>
						</xsl:when>
						<xsl:when test="../../pass_info/error[@field_name = current()/@name and @name = 'UNEXISTED_VALUE']">
							<div class="error">
								<span>Выбрано несуществующее значение. Попробуйте ещё раз</span>
							</div>
						</xsl:when>
					</xsl:choose>
				</xsl:if>
				<xsl:if test="@comment != ''">
					<div class="comment-">
						<xsl:value-of select="@comment"/>
					</div>
				</xsl:if>
			</div>
			<div class="field" id="group_{current()/@name}_{@value}">
				<label for="item_{current()/@name}_{@value}">
					<xsl:value-of select="current()/@group_title"/>
					<xsl:text>:&#160;</xsl:text>
					<xsl:if test="current()/@is_important = 1">
						<span class="star">*</span>
					</xsl:if>
				</label>
				<input class="string-" type="text" name="{current()/@group_other_name}" value="{$document_field/@group_other_value}">
					<xsl:if test="../../pass_info/vars/var[@name = current()/@group_other_name]">
						<xsl:attribute name="value">
							<xsl:value-of select="../../pass_info/vars/var[@name = current()/@group_other_name]"/>
						</xsl:attribute>
					</xsl:if>
				</input>
				<xsl:if test="../../pass_info/error[@field_name = current()/@group_other_name]">
					<xsl:choose>
						<xsl:when test="../../pass_info/error[@field_name = current()/@group_other_name and @name = 'UNFILLED']">
							<div class="error">
								<span>Это обязательное поле, и оно должно быть заполнено</span>
							</div>
						</xsl:when>
						<xsl:when test="../../pass_info/error[@field_name = current()/@group_other_name and @name = 'UNEXISTED_VALUE']">
							<div class="error">
								<span>Выбрано несуществующее значение. Попробуйте ещё раз</span>
							</div>
						</xsl:when>
					</xsl:choose>
				</xsl:if>
				<xsl:if test="current()/@is_important = 1">
					<div class="comment-">
						<xsl:text>Необходимо заполнить, если выбран пункт "Другое.."</xsl:text>
					</div>
				</xsl:if>
			</div>
			<div class="field" id="item_{current()/@name}_{@value}">
				<label for="item_{current()/@name}_{@value}">
					<xsl:value-of select="current()/@item_title"/>
					<xsl:text>:&#160;</xsl:text>
					<xsl:if test="current()/@is_important = 1">
						<span class="star">*</span>
					</xsl:if>
				</label>
				<input class="string-" type="text" name="{current()/@item_other_name}" value="{$document_field/@item_other_value}">
					<xsl:if test="../../pass_info/vars/var[@name = current()/@item_other_name]">
						<xsl:attribute name="value">
							<xsl:value-of select="../../pass_info/vars/var[@name = current()/@item_other_name]"/>
						</xsl:attribute>
					</xsl:if>
				</input>
				<xsl:if test="$module/pass_info/error[@field_name = current()/@item_other_name]">
					<xsl:choose>
						<xsl:when test="../../pass_info/error[@field_name = current()/@item_other_name and @name = 'UNFILLED']">
							<div class="error">
								<span>Это обязательное поле, и оно должно быть заполнено</span>
							</div>
						</xsl:when>
						<xsl:when test="../../pass_info/error[@field_name = current()/@item_other_name and @name = 'UNEXISTED_VALUE']">
							<div class="error">
								<span>Выбрано несуществующее значение. Попробуйте ещё раз</span>
							</div>
						</xsl:when>
					</xsl:choose>
				</xsl:if>
				<xsl:if test="current()/@is_important = 1">
					<div class="comment-">
						<xsl:text>Необходимо заполнить, если выбран пункт "Другое.."</xsl:text>
					</div>
				</xsl:if>
			</div>
		</noscript>
	</xsl:template>
</xsl:stylesheet>
